<?php


namespace App\Http\Backend\Controllers;


use App\Http\Models\Users;
use App\Http\Utils\Token;
use Illuminate\Http\Request;

class DashboardController
{
    public function login(Request $request)
    {

        $username = $request->post('username');
        $password = $request->post('password');
        $user =Users::query()->where('username','=',$username)->first();

        if(empty($user)){
            return fail("账户不存在");
        }
        if(password_verify($password,$user->password)){
            $token=Token::getInstance()->generate($user->fillModel())->save(function ($token)use ($user){
                $user->token=$token;
                $user->save();
            });
            if($token->getError()){
                return fail($token->getError());
            }
            return success(['token'=>Token::getInstance()->getToken()]);
        }else{
            return fail("密码错误");
        }
    }

}